package com.pifuke.patient.ui;

import java.util.ArrayList;

import org.json.JSONArray;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import cn.o.app.json.JsonUtil;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;

import com.evan.common.utils.CommonUtility;
import com.pifuke.patient.R;
import com.smiier.skin.DoctorDetailActivity;
import com.smiier.skin.adapter.KepuDoctorAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.net.KepuGetDoctorListTask;
import com.smiier.skin.net.KepuGetDoctorListTask.KepuGetDoctorListRequest;
import com.smiier.skin.net.KepuGetDoctorListTask.KepuGetDoctorListResponse;
import com.smiier.skin.ui.BasicStateView;
import com.smiier.skin.util.GlobalSettings;
import com.umeng.analytics.MobclickAgent;

/**
 * 疾病推荐医生
 * 
 * @author Lennon
 * 
 */
public class SymptomDoctorView extends BasicStateView implements
		OnItemClickListener {

	private ListView list_morentuijian;

	private KepuDoctorAdapter kepuDoctorAdapter;

	ArrayList<Object> array_doctor = new ArrayList<Object>();

	public SymptomDoctorView(Context context) {
		super(context);
	}

	public SymptomDoctorView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SymptomDoctorView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		setContentView(R.layout.view_symptom_doctor);

		list_morentuijian = findViewById(R.id.list_morentuijian, ListView.class);
		list_morentuijian.setOnItemClickListener(this);
		loadData();
	}

	private void loadData() {
		if (GlobalSettings.symptomItemInfo.Doctor_Uids != null
				&& !GlobalSettings.symptomItemInfo.Doctor_Uids.isEmpty()) {

			KepuGetDoctorListRequest request = new KepuGetDoctorListRequest();

			request.doctorid = GlobalSettings.symptomItemInfo.Doctor_Uids;

			KepuGetDoctorListTask task = new KepuGetDoctorListTask();
			task.setRequest(request);
			task.addListener(new NetTaskListener<KepuGetDoctorListTask.KepuGetDoctorListRequest, KepuGetDoctorListTask.KepuGetDoctorListResponse>() {

				@Override
				public void onComplete(
						INetTask<KepuGetDoctorListRequest, KepuGetDoctorListResponse> task,
						KepuGetDoctorListResponse response) {

					if (response.ResultCode != 200) {
						return;
					}

					try {
						o(new JSONArray(JsonUtil.convert(response.Res)));
					} catch (Exception e) {
					}

				}

				@Override
				public void onException(
						INetTask<KepuGetDoctorListRequest, KepuGetDoctorListResponse> task,
						Exception e) {
					e.printStackTrace();

				}

			});
			add(task);

		}

		// mRequest.token = GlobalSettings.sToken;
		/*
		 * DoctorUserGetListTask task = new DoctorUserGetListTask();
		 * task.setRequest(mRequest); task.addListener(new
		 * NetTaskListener<DoctorUserGetListTask.DoctorUserGetListRequest,
		 * DoctorUserGetListTask.DoctorUserGetListResponse>() {
		 * 
		 * @Override public void onException( INetTask<DoctorUserGetListRequest,
		 * DoctorUserGetListResponse> task, Exception e) { e.printStackTrace();
		 * list_morentuijian.doComplete(); }
		 * 
		 * @Override public void onComplete( INetTask<DoctorUserGetListRequest,
		 * DoctorUserGetListResponse> task, DoctorUserGetListResponse response)
		 * { list_morentuijian.doComplete(); if (response.ResultCode != 200) {
		 * return; } try { o(new
		 * JSONArray(JsonUtil.convert(response.Res.Users))); } catch (Exception
		 * e) {
		 * 
		 * } } }); add(task);
		 */
	}

	protected void o(JSONArray array) {
		JSONArray arr = array;

		// }

		CommonUtility.putAll(array_doctor, arr, true);

		if (CommonUtility.isNull(kepuDoctorAdapter)) {
			kepuDoctorAdapter = new KepuDoctorAdapter((Activity) getContext(),
					array_doctor, true);
			list_morentuijian.setAdapter(kepuDoctorAdapter);
		} else {
			kepuDoctorAdapter.notifyUpdate();
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		Intent intent = new Intent(getContext(), DoctorDetailActivity.class);
		try {
			intent.putExtra(Constant.IDENTITY_KEY, array_doctor.get(position)
					.toString());
			getContext().startActivity(intent);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

/*	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(getContext());
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(getContext());
	}*/
	

}
